package com.oa.framework.acl.url.service;

import com.oa.common.tree.TreeUtils;
import com.oa.framework.acl.url.dao.ResourceManagementDao;
import com.oa.framework.acl.url.dao.SystemUrlResourceDao;
import com.oa.framework.acl.url.domain.ResourceManagement;
import com.oa.framework.acl.url.domain.SystemUrlResource;
import com.oa.framework.cache.ResourceManagementCache;
import com.oa.framework.cache.UrlResourceCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author cuichao
 */
@Service
public class ResourceManagementServiceImpl implements ResourceManagementService {

    private final List<ResourceManagement> empty = new ArrayList<>();

    @Autowired
    private SystemUrlResourceDao resourceDao;

    @Autowired
    private ResourceManagementDao managementDao;

    @Autowired
    private UrlResourceCache resourceCache;

    @Autowired
    private ResourceManagementCache managementCache;





    @Override
    public List<ResourceManagement> loadResourceManagement() {

        List<ResourceManagement> fold = managementCache.get();

        if (fold != null) {
            return fold;
        }
        List<ResourceManagement> managements = this.managementDao.loadResourceManagement();
        if (CollectionUtils.isEmpty(managements)) {
            return empty;
        }
        //fold = foldManagement(managements);
        fold = TreeUtils.buildTree(managements, null);

        managementCache.put(fold);
        return fold;
    }


    @Override
    public boolean createResource(SystemUrlResource resource) {
        //清楚 资源和管理器的缓存
        resourceCache.remove();
        managementCache.remove();
        int insert = resourceDao.insert(resource);

        if (insert == 1) {
            return true;
        }
        return false;
    }

    /**
     * 折叠资源管理器
     *
     * @param managements
     * @return
     */
    private List<ResourceManagement> foldManagement(List<ResourceManagement> managements) {
        List<ResourceManagement> fold = new ArrayList<>();
        //find root
        for (ResourceManagement management : managements) {
            if (management.getParentId() == null || management.getParentId() <= 0) {
                fold.add(management);
                recursionManagement(managements, management);
            }
        }
        return fold;
    }

    /**
     * 递归
     *
     * @param managements
     * @param parent
     */
    private void recursionManagement(List<ResourceManagement> managements, ResourceManagement parent) {
        if (parent.hasLeaf()) {
            return;
        }

        for (ResourceManagement management : managements) {
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList<>());
            }
            if (parent.getManagementId().equals(management.getParentId())) {
                parent.getChildren().add(management);
                recursionManagement(managements, management);
            }

        }

    }


}
